<?php

namespace app\admin\controller;

use app\admin\model\KefuAccount;
use app\admin\model\KefuGroup;
use app\admin\model\KefuUser;
use app\admin\model\UserInfo;
use app\common\controller\admin\Base;
use think\Cache;
use think\Db;

class Kefu extends Base
{


    protected $noNeedLogin = ['login'];

    protected $kefuGroupmodel;
    protected $kefuUsermodel;
    protected $kefuAccountmodel;
    protected $userModel;



    public function _initialize()
    {
        parent::_initialize();
        $this->kefuGroupmodel = new KefuGroup();
        $this->kefuUsermodel = new KefuUser();
        $this->userModel = new UserInfo();
        $this->kefuAccountmodel = new KefuAccount();
    }

    //添加客服组
    public function addKefuGroup()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["name"])) {
                $this->error('请输入客服组名称');
            }

            $data = [
                'sort' => isset($params["sort"]) ? $params["sort"] : 1,
                'name' => $params["name"],
                'kefu_num' => isset($params["kefu_num"]) ? $params["kefu_num"] : 0,
                'partner' => isset($params["partner"]) ? $params["partner"] : "",
            ];
            if (!empty($params["kefu_username"]) && !empty($params["kefu_password"])) {

                $model = new KefuUser();
                $groupModel = new KefuGroup();
                $groupInfo = $groupModel->where('name', '=', $params["name"])->where('is_del', '=', 0)->where('status', '=', 1)->where('partner', '=', $data['partner'])->find();
                if ($groupInfo) {
                    $leader = $model->where('status', '=', 1)->where('is_leader', '=', 1)->where('kefu_group_id', '=', $groupInfo['id'])->find();
                    if(!empty($leader)){
                        $this->success("该客服组已经有对应组长，请添加客服人员即可");
                    }
                }

            }

            $ret = KefuGroup::create($data);
            $id = $ret->getLastInsID();
            //添加到队列分配客服中
            $key = "kefuGroup";
            Cache::store('redis')->lpush($key, $id);
            //增加相应的客服组长
            if (!empty($params["kefu_username"]) && !empty($params["kefu_password"])) {

                $kefuUserLeader = [
                    'username' => $params["kefu_username"],
                    'password' => md5("NRJ*@".$params["kefu_password"]),
                    'kefu_group_id' => $id,
                    'is_leader' => 1,
                ];
                $kefuUserRet = KefuUser::create($kefuUserLeader);
                $kefuUserId = $kefuUserRet->getLastInsID();
                //添加到队列分配客服中
                $kefuKey = "kefu_".$id."_user";
                Cache::store('redis')->lpush($kefuKey, $kefuUserId);
            }

            $this->success("添加成功");
        }
    }
    //删除客服组
    public function delKefuGroup()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }


            $where['id'] = $params["id"];
            $this->kefuGroupmodel->update(['is_del' => 1], $where);
            $this->success("添加成功");
        }
    }
    //获取客服组

    public function getKefuGroupList()
    {

        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        $params = $this->request->get();
        $name = "";
        if (!empty($params["name"])) {
            $name = $params["name"];
        }


        $page = isset($params["page"]) ? $params["page"] : 1;
        $pageSize = isset($params["pageSize"]) ? $params["pageSize"] : 10;
        $offset = ($page -1) * $pageSize;
        $query = Db::table('su_kefu_group')->where('is_del', '=', 0)->page($offset, $pageSize);
        if ($name) {
            if (isset($params["status"])) {
                $result = $query->where('name', 'like', '%'.$name.'%')->order('id','desc')->where('status', '=', $params["status"])->select()->toArray();
            } else {
                $result = $query->where('name', 'like', '%'.$name.'%')->order('id','desc')->select()->toArray();
            }

        } else {
            if (isset($params["status"])) {
                $result = $query->where('status', '=', $params["status"])->order('id','desc')->select()->toArray();
            } else {
                $result = $query->order('id','desc')->select()->toArray();
            }

        }
        $count = Db::table('su_kefu_group')->where('is_del', '=', 0)->count('id');
        if ($name) {
            if (isset($params["status"])) {
                $count = Db::table('su_kefu_group')->where('name', 'like', '%'.$name.'%')->where('status', '=', $params["status"])->where('is_del', '=', 0)->count('id');
            } else {
                $count = Db::table('su_kefu_group')->where('name', 'like', '%'.$name.'%')->where('is_del', '=', 0)->count();
            }

        }
        //获取当前组分配数量
        $ids = array_column((array)$result, 'id');
        $kefuGroupCntList = Db::table('su_user_info')->field("count(id) as groupCnt, kefu_group_id")->where('kefu_group_id', 'in', implode(",", $ids))->where('is_del', '=', 0)->group('kefu_group_id')->select()->toArray();
        $kefuGroupCntListData = array_column((array)$kefuGroupCntList, 'groupCnt', 'kefu_group_id');


        $list = [];
        foreach ($result as $val) {
            $val['used_group_cnt'] = isset($kefuGroupCntListData[$val['id']]) ? $kefuGroupCntListData[$val['id']] : 0;
            $list[] = $val;
        }
        $data['count'] = $count;
        $data['list'] = $list;
        $this->success("","", $data);

    }

    public function getKefuGroupInfo()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        $params = $this->request->get();
        if (empty($params["id"])) {
            $this->error('请输入客服组id');

        }
        $id = $params["id"];
        $result = Db::table('su_kefu_group')->where('id', '=', $id)->find();
        $kefuGroupCnt = Db::table('su_user_info')->where('kefu_group_id', '=', $id)->where('is_del', '=', 0)->count();
        $result['used_group_cnt'] = $kefuGroupCnt;
        $this->success("","", $result);

    }
    //修改客服组
    public function upKefuGroupInfo()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }
            $id = $params["id"];
            $data = [];
            if (!empty($params["name"])) {
                $data['name'] = $params["name"];
            }
            if (!empty($params["partner"])) {
                $data['partner'] = $params["partner"];
            }


            if (isset($params["sort"])) {
                $data['sort'] = $params["sort"];
            }
            if (isset($params["status"]) && in_array($params["status"], [0, 1])) {
                $data['status'] = $params["status"];
            }
            if ($data) {
                $data['id'] = $id;

                $result = Db::table('su_kefu_group')->update($data);
            }

            $this->success("修改成功");

        }

    }

    public function disableKefuGroup()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['status'] = 0;
            Db::table('su_kefu_group')->update($data);
            $this->success("禁用成功");
        }

    }

    public function enableKefuGroup()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['status'] = 1;
            Db::table('su_kefu_group')->update($data);
            $this->success("启用成功");
        }

    }


    //添加客服组
    public function addKefuUser()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["username"])) {
                $this->error('请输入客服人员名称');
            }
            if (empty($params["password"])) {
                $this->error('请输入客服人员密码');
            }
            if (empty($params["customers_num"])) {
                $this->error('请输入客服人员可分配客户数量');
            }
//            if (empty($params["mobile"])) {
//                $this->error('请输入客服人员手机号');
//            }

            if (empty($params["kefu_group_id"])) {
                $this->error('请输入对应客服组id');
            }
            $password = md5("NRJ*@".$params["password"]);

            $data = [
                'sort' => isset($params["sort"]) ? $params["sort"] : 1,
                'username' => $params["username"],
                'password' => $password,
                'kefu_group_id' => $params["kefu_group_id"],
                'mobile' => isset($params["mobile"]) ? $params["mobile"] : 0,
                'is_excess_switch' => isset($params["is_excess_switch"]) ? $params["is_excess_switch"] : 0,
                'customers_num' => $params["customers_num"],
            ];
            $ret = KefuUser::create($data);
            $id = $ret->getLastInsID();
            //添加到队列分配客服中
            $key = "kefu_".$params["kefu_group_id"]."_user";
            Cache::store('redis')->lpush($key, $id);
            $this->success("添加成功");
        }
    }
    //删除客服组
    public function delKefuUser()
    {

        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }


            $where['id'] = $params["id"];
            $this->kefuUsermodel->update(['is_del' => 1], $where);
            $this->success("添加成功");
        }
    }
    //获取客服组

    public function getKefuUserList()
    {

        $params = $this->request->get();
        $name = "";
        if (!empty($params["username"])) {
            $name = $params["username"];
        }
        $page = isset($params["page"]) ? $params["page"] : 1;
        $pageSize = isset($params["pageSize"]) ? $params["pageSize"] : 10;

        $offset = ($page -1) * $pageSize;
        $query = Db::table('su_kefu_user');
        if ($name) {
            $query = $query->where('username', 'like', '%'.$name.'%');
        }
        if ($this->is_kefu && $this->is_kefu_leader) {
            $query = $query->where('kefu_group_id', '=', $this->info['kefu_group_id']);
        }
        if ($this->is_kefu && !$this->is_kefu_leader) {
            $query = $query->where('id', '=', $this->info['id']);
        }
        if ($this->is_admin) {
            $kefu_group_id_par = isset($params["kefu_group_id"]) ? $params["kefu_group_id"] : 0;
            if ($kefu_group_id_par) {
                $query = $query->where('kefu_group_id', '=', $kefu_group_id_par);
            }

        }
        $result = $query->where('is_del', '=', 0)->page($offset, $pageSize)->order('id','desc')->select()->toArray();


        $queryCnt = Db::table('su_kefu_user');
        if ($name) {
            $queryCnt = $queryCnt->where('username', 'like', '%'.$name.'%');
        }
        if ($this->is_kefu && $this->is_kefu_leader) {
            $queryCnt = $queryCnt->where('kefu_group_id', '=', $this->info['kefu_group_id']);
        }
        if ($this->is_kefu && !$this->is_kefu_leader) {
            $queryCnt = $queryCnt->where('id', '=', $this->info['id']);
        }
        if ($this->is_admin) {
            $kefu_group_id_par = isset($params["kefu_group_id"]) ? $params["kefu_group_id"] : 0;
            if ($kefu_group_id_par) {
                $queryCnt = $queryCnt->where('kefu_group_id', '=', $kefu_group_id_par);
            }

        }
        $count = $queryCnt->where('is_del', '=', 0)->count('id');

        $ids = array_column((array)$result, 'id');
        $kefuGroupIds = array_column((array)$result, 'kefu_group_id');
        $kefuCntList = Db::table('su_user_info')->field("count(id) as kefuCnt, customer_service_account_middle_id")->where('customer_service_account_middle_id', 'in', implode(",", $ids))->where('is_del', '=', 0)->group('customer_service_account_middle_id')->select()->toArray();
        $kefuCntListData = array_column((array)$kefuCntList, 'kefuCnt', 'customer_service_account_middle_id');
        $kefuGroup = Db::table('su_kefu_group')->where('id', 'in', $kefuGroupIds)->select()->toArray();
        $kefuGroupData = array_column($kefuGroup, null, 'id');

        $list = [];
        foreach ($result as $val) {
            $val['used_cnt'] = isset($kefuCntListData[$val['id']]) ? $kefuCntListData[$val['id']] : 0;
            $kefu_group_id = $val['kefu_group_id'];
            $val['kefu_group_name'] = "";
            $val['partner'] = "";
            if (isset($kefuGroupData[$kefu_group_id])) {
                $kefuGroupDataTmp = $kefuGroupData[$kefu_group_id];
                $val['kefu_group_name']  = $kefuGroupDataTmp['name'];
                $val['partner'] = $kefuGroupDataTmp['partner'];
            }
            $list[] = $val;
        }

        $data['count'] = $count;
        $data['list'] = $list;
        //获取客服已经分配的用户


        $this->success("","", $data);

    }

    public function getKefuUserInfo()
    {
        $params = $this->request->get();
        if (empty($params["id"])) {
            $this->error('请输入客服人员id');

        }
        $id = $params["id"];
        $result = Db::table('su_kefu_user')->where('id', '=', $id)->find();
        $this->success("","", $result);

    }
    //修改客服组
    public function upKefuUserInfo()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服人员id');
            }
            $id = $params["id"];
            $data = [];
            if (!empty($params["username"])) {
                $data['username'] = $params["username"];
            }
            if (!empty($params["mobile"])) {
                $data['mobile'] = $params["mobile"];
            }
            if (!empty($params["customers_num"])) {
                $data['customers_num'] = $params["customers_num"];
            }
            if (!empty($params["kefu_group_id"])) {
                $data['kefu_group_id'] = $params["kefu_group_id"];
            }
            if (isset($params["is_excess_switch"]) && in_array($params["is_excess_switch"], [0,1])) {
                $data['is_excess_switch'] = $params["is_excess_switch"];
            }
            if (!empty($params["password"])) {
                $data['password'] = md5("NRJ*@".$params["password"]);
            }
            if (isset($params["sort"])) {
                $data['sort'] = $params["sort"];
            }
            if (isset($params["status"]) && in_array($params["status"], [0,1])) {
                $data['status'] = $params["status"];
            }
            $data['id'] = $id;
            Db::table('su_kefu_user')->update($data);
            $this->success("修改成功");

        }

    }

    public function disableKefuUser()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服人员id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['status'] = 0;
            Db::table('su_kefu_user')->update($data);
            $this->success("禁用成功");
        }

    }

    public function enableKefuUser()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服人员id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['status'] = 1;
            Db::table('su_kefu_user')->update($data);
            $this->success("启用成功");
        }

    }

    public function disableKefuUserChao()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服人员id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['is_excess_switch'] = 0;
            Db::table('su_kefu_user')->update($data);
            $this->success("禁用成功");
        }

    }

    public function enableKefuUserChao()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服人员id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['is_excess_switch'] = 1;
            Db::table('su_kefu_user')->update($data);
            $this->success("启用成功");
        }

    }

    public function getUserList()
    {
        $params = $this->request->get();
        $where = [];
        if ($this->is_kefu && !$this->is_kefu_leader) {
            $where['customer_service_account_middle_id'] = $this->info['id'];
        }
        if ($this->is_kefu && $this->is_kefu_leader) {
            $where['kefu_group_id'] = $this->info['kefu_group_id'];
        }
        if (!empty($params["username"])) {
            $where['username'] = $params["username"];
        }
        if (!empty($params["mobile"])) {
            $where['mobile'] = $params["mobile"];
        }
        $time = isset($params["startTime"]) ? date('Y-m-d', strtotime($params["startTime"])) : "";
        $endTime = isset($params["endTime"]) ? date('Y-m-d 23:59:59', strtotime($params["endTime"])) : "";

        if (!empty($time) && !empty($endTime)) {
            $where['create_time'] = ['between', [$time, $endTime]];
        }
        $page = isset($params["page"]) ? $params["page"] : 1;
        $pageSize = isset($params["pageSize"]) ? $params["pageSize"] : 10;
        $offset = ($page -1) * $pageSize;
        $list = Db::table('su_user_info')->where($where)->page($offset, $pageSize)->order('id','desc')->select()->toArray();
        $count = Db::table('su_user_info')->where($where)->count();
        $kefuIdss = array_column($list, 'customer_service_account_middle_id');
        $kefuGroupIds = array_column($list, 'kefu_group_id');
        $kefuUser = Db::table('su_kefu_user')->where('id', 'in', $kefuIdss)->select()->toArray();
        $kefuGroup = Db::table('su_kefu_group')->where('id', 'in', $kefuGroupIds)->select()->toArray();
        $kefuUserData = array_column($kefuUser, null, 'id');
        $kefuGroupData = array_column($kefuGroup, null, 'id');

        $content = [];
        foreach ($list as $val) {
            $customer_service_account_middle_id = $val['customer_service_account_middle_id'];
            $kefu_group_id = $val['kefu_group_id'];
            $val['kefu_name'] = "";
            $val['kefu_group_name'] = "";
            $val['partner'] = "";
            if (isset($kefuUserData[$customer_service_account_middle_id])) {
                $kefuUserDataTmp = $kefuUserData[$customer_service_account_middle_id];
                $val['kefu_name']  = $kefuUserDataTmp['username'];
            }
            if (isset($kefuGroupData[$kefu_group_id])) {
                $kefuGroupDataTmp = $kefuGroupData[$kefu_group_id];
                $val['kefu_group_name']  = $kefuGroupDataTmp['name'];
                $val['partner'] = $kefuGroupDataTmp['partner'];
            }
            $content[] = $val;
        }
        $data['count'] = $count;
        $data['list'] = $content;
        $this->success("", "", $data);

    }

    public function login()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params['username'])) {
                $this->error('请输入管理员名称');
            }
            if (empty($params['password'])) {
                $this->error('请输入管理员密码');
            }
            $result = Db::table('su_admin_user')->where('password', '=', md5($this->adminLoginSalt.$params['password']))->where('username', '=', $params['username'])->find();
            $access_token = md5(uniqid() . mt_rand());
            $result['token'] = $access_token;
            $result['is_admin'] = 1;
            $result['is_kefu'] = 0;
            $result['is_leader'] = 0;
            Cache::set($access_token, $result, 86400 * 100);
            $this->success("", "", $result);
        }
    }

    //添加客服账号
    public function addKefuAccount()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["kefu_account"])) {
                $this->error('请输入客服账号');
            }
            if (empty($params["kefu_user_id"])) {
                $this->error('请输入客服人员id');
            }
            if (empty($params["kefu_group_id"])) {
                $this->error('请输入客服组id');
            }
            if (empty($params["app_name"])) {
                $this->error('请输入app');
            }
            if (empty($params["short_url"])) {
                $this->error('请输入短链');
            }

//            if (empty($params["air_url"])) {
//                $this->error('请输入飞机链接');
//            }

            $data = [
                'air_url' => isset($params["air_url"]) ? $params["air_url"] : "",
                'kefu_account' => $params["kefu_account"],
                'kefu_user_id' => $params["kefu_user_id"],
                'app_name' => $params["app_name"],
                'short_url' => $params["short_url"],
                'kefu_group_id' => $params["kefu_group_id"],
            ];
            $ret = KefuAccount::create($data);
            $id = $ret->getLastInsID();
            $data['id'] = $id;
            $this->success("添加成功", "", $data);
        }
    }

    public function delKefuAccount()
    {

        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }


            $where['id'] = $params["id"];
            $this->kefuAccountmodel->update(['is_del' => 1], $where);
            $this->success("添加成功");
        }
    }

    public function getKefuAccountList()
    {

        $params = $this->request->get();
        $name = "";
        if (!empty($params["kefu_account"])) {
            $name = $params["kefu_account"];
        }
        $page = isset($params["page"]) ? $params["page"] : 1;
        $pageSize = isset($params["pageSize"]) ? $params["pageSize"] : 10;
        $offset = ($page -1) * $pageSize;
        $query = Db::table('su_kefu_account');
        if ($name) {
            $query = $query->where('kefu_account', 'like', '%'.$name.'%');
        }
        if ($this->is_kefu && $this->is_kefu_leader) {
            $query = $query->where('kefu_group_id', '=', $this->info['kefu_group_id']);
        }
        if ($this->is_kefu && !$this->is_kefu_leader) {
            $query = $query->where('kefu_user_id', '=', $this->info['id']);
        }
        if ($this->is_admin) {
            $kefu_group_id_par = isset($params["kefu_group_id"]) ? $params["kefu_group_id"] : 0;
            if ($kefu_group_id_par) {
                $query = $query->where('kefu_group_id', '=', $kefu_group_id_par);
            }
            $kefu_user_id_par = isset($params["kefu_user_id"]) ? $params["kefu_user_id"] : 0;
            if ($kefu_user_id_par) {
                $query = $query->where('kefu_user_id', '=', $kefu_user_id_par);
            }

        }
        $result = $query->where('is_del', '=', 0)->page($offset, $pageSize)->order('id','desc')->select()->toArray();


        $queryCnt = Db::table('su_kefu_account');
        if ($name) {
            $queryCnt = $queryCnt->where('kefu_account', 'like', '%'.$name.'%');
        }
        if ($this->is_kefu && $this->is_kefu_leader) {
            $queryCnt = $queryCnt->where('kefu_group_id', '=', $this->info['kefu_group_id']);
        }

        if ($this->is_kefu && !$this->is_kefu_leader) {
            $queryCnt = $queryCnt->where('kefu_user_id', '=', $this->info['id']);
        }
        if ($this->is_admin) {
            $kefu_group_id_par = isset($params["kefu_group_id"]) ? $params["kefu_group_id"] : 0;
            if ($kefu_group_id_par) {
                $queryCnt = $queryCnt->where('kefu_group_id', '=', $kefu_group_id_par);
            }
            $kefu_user_id_par = isset($params["kefu_user_id"]) ? $params["kefu_user_id"] : 0;
            if ($kefu_user_id_par) {
                $queryCnt = $queryCnt->where('kefu_user_id', '=', $kefu_user_id_par);
            }

        }
        $count = $queryCnt->where('is_del', '=', 0)->count('id');

        $kefuGroupIds = array_column((array)$result, 'kefu_group_id');
        $kefuUserIds = array_column((array)$result, 'kefu_user_id');
        $kefuGroup = Db::table('su_kefu_group')->where('id', 'in', $kefuGroupIds)->select()->toArray();
        $kefuGroupData = array_column($kefuGroup, null, 'id');
        $kefuUser = Db::table('su_kefu_user')->where('id', 'in', $kefuUserIds)->select()->toArray();
        $kefuUserData = array_column($kefuUser, null, 'id');

        $list = [];
        //获取组长
        $kefuGroupZu = Db::table('su_kefu_user')->where('kefu_group_id', 'in', $kefuGroupIds)->where('is_leader', '=', 1)->select()->toArray();
        $kefuGroupZuData = array_column($kefuGroupZu, null, 'kefu_group_id');
        foreach ($result as $val) {
            $kefu_user_id = $val['kefu_user_id'];
            $kefu_group_id = $val['kefu_group_id'];
            $val['kefu_name'] = "";
            $val['kefu_group_name'] = "";
            $val['partner'] = "";
            $val['group_leader_name'] = "";
            if (isset($kefuUserData[$kefu_user_id])) {
                $kefuUserDataTmp = $kefuUserData[$kefu_user_id];
                $val['kefu_name']  = $kefuUserDataTmp['username'];
            }
            if (isset($kefuGroupData[$kefu_group_id])) {
                $kefuGroupDataTmp = $kefuGroupData[$kefu_group_id];
                $val['kefu_group_name']  = $kefuGroupDataTmp['name'];
                $val['partner'] = $kefuGroupDataTmp['partner'];
            }
            if (isset($kefuGroupZuData[$kefu_group_id])) {
                $kefuGroupZuDataTmp = $kefuGroupZuData[$kefu_group_id];
                $val['group_leader_name']  = $kefuGroupZuDataTmp['username'];
            }
            $list[] = $val;
        }

        $data['count'] = $count;
        $data['list'] = $list;
        //获取客服已经分配的用户


        $this->success("","", $data);

    }

    public function getKefuAccountInfo()
    {
        $params = $this->request->get();
        if (empty($params["id"])) {
            $this->error('请输入客服账号id');

        }
        $id = $params["id"];
        $result = Db::table('su_kefu_account')->where('id', '=', $id)->find();
        $groupInfo = Db::table('su_kefu_group')->where('id', '=', $result['kefu_group_id'])->find();
        $kefuUserInfo = Db::table('su_kefu_user')->where('id', '=', $result['kefu_user_id'])->find();
        $result['kefu_group_name'] = $groupInfo['name'];
        $result['partner'] = $groupInfo['partner'];
        $result['kefu_name'] = $kefuUserInfo['username'];
        $this->success("","", $result);

    }
    //修改客服组
    public function upKefuAccountInfo()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服人员id');
            }
            $id = $params["id"];
            $data = [];

            if (!empty($params["kefu_account"])) {
                $data['kefu_account'] = $params["kefu_account"];
            }
            if (!empty($params["kefu_user_id"])) {
                $data['kefu_user_id'] = $params["kefu_user_id"];
            }
            if (!empty($params["kefu_group_id"])) {
                $data['kefu_group_id'] = $params["kefu_group_id"];
            }
            if (!empty($params["short_url"])) {
                $data['short_url'] = $params["short_url"];
            }
            if (!empty($params["air_url"])) {
                $data['air_url'] = $params["air_url"];
            }
            if (!empty($params["app_name"])) {
                $data['app_name'] = $params["app_name"];
            }
            if ($data) {
                $data['id'] = $id;
                Db::table('su_kefu_account')->update($data);
            }

            $this->success("修改成功");

        }

    }

    public function disableKefuAccount()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服账号id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['status'] = 0;
            Db::table('su_kefu_account')->update($data);
            $this->success("禁用成功");
        }

    }

    public function enableKefuAccount()
    {
        if (($this->is_kefu && !$this->is_kefu_leader) || !$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服账号id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['status'] = 1;
            Db::table('su_kefu_account')->update($data);
            $this->success("启用成功");
        }

    }

    public function enableUserIsSwitchKefuStatus()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服账号id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['is_check_kf_switch'] = 1;
            Db::table('su_user_info')->update($data);
            $this->success("操作成功");
        }
    }
    public function disableUserIsSwitchKefuStatus()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服账号id');
            }
            $id = $params["id"];
            $data['id'] = $id;
            $data['is_check_kf_switch'] = 0;
            Db::table('su_user_info')->update($data);
            $this->success("操作成功");
        }
    }


}
